﻿using AntdUI;
using NetAutoGUI;

namespace AutoPatientInfoRecorder
{
    public partial class Main : AntdUI.Window
    {
        public Main()
        {
            InitializeComponent();
        }
        private void Main_Load(object sender, EventArgs e)
        {
            tab_PatientInfo.Columns = new Column[] {
           new ColumnCheck("Check"){ Fixed=true},
           new("Name","姓名"){ Fixed=true},
           new("Sex","性别"){ Fixed=true},
           new("Age","年龄",ColumnAlign.Center),
           new("Address","住址"),
           new("Doctor","医生"),
           new("Res","诊断"),
           new("Fee","费用"),
           };
        }
        private void Btn_mode_Click(object sender, EventArgs e)
        {
            Config.IsDark = !Config.IsDark;
            Dark = Config.IsDark;
            if (Dark)
            {
                btn_mode.ImageSvg = Properties.Resources.app_dark;
                BackColor = Color.Black;
                ForeColor = Color.White;
            }
            else
            {
                btn_mode.ImageSvg = Properties.Resources.app_light;
                BackColor = Color.White;
                ForeColor = Color.Black;
            }
            OnSizeChanged(e);
        }
        private void Btn_SearchPatient_Click(object sender, EventArgs e)
        {
            var list = new List<PatientInfo>(10) {
                new ("测试男","男",18,"广东省深圳市南山区科苑路科苑路88号","测试医生1","消化不良",188),
                new ("测试女","女",24,"广州天河龙洞植物园","测试医生2","轻度过敏",166)
            };
            tab_PatientInfo.DataSource = list;
        }
        public void Btn_AddNew_Click(object sender, EventArgs e)
        {
            if (tab_PatientInfo.DataSource is List<PatientInfo> dataSource)
            {
                dataSource = dataSource.Where(item => item.Check).ToList();
                if (dataSource.Count > 0)
                {
                    NetAutoGUI.Window win = GUI.Application.FindWindowLikeTitle("*预约*");
                    if (win != null)
                    {
                        win.Activate();
                        Thread.Sleep(1000);
                        var addbtn = GUI.Screenshot.LocateAllOnScreen("image/增加.png").First();
                        Thread.Sleep(200);
                        GUI.Mouse.Click(addbtn.X, addbtn.Y);
                        Thread.Sleep(600);
                        var name = GUI.Screenshot.LocateAllOnScreen("image/姓名.png").First();
                        GUI.Mouse.Click(name.X + 80, name.Y + 15);
                        Thread.Sleep(200);
                        GUI.Keyboard.Write(dataSource[0].Name);
                        Thread.Sleep(200);
                        if (dataSource[0].Sex == "男")
                        {
                            var boy = GUI.Screenshot.LocateAllOnScreen("image/男.png").First();
                            Thread.Sleep(200);
                            GUI.Mouse.Click(boy.X, boy.Y);
                        }
                        else
                        {
                            var girs = GUI.Screenshot.LocateAllOnScreen("image/女.png").First();
                            Thread.Sleep(200);
                            GUI.Mouse.Click(girs.X, girs.Y);
                        }
                        Thread.Sleep(200);
                        var age = GUI.Screenshot.LocateAllOnScreen("image/年龄.png").First();
                        Thread.Sleep(200);
                        GUI.Mouse.Click(age.X + 20, age.Y + 15);
                        Thread.Sleep(200);
                        GUI.Keyboard.Write(dataSource[0].Age.ToString());
                        Thread.Sleep(200);
                        var doctor = GUI.Screenshot.LocateAllOnScreen("image/医生.png").First();
                        Thread.Sleep(200);
                        GUI.Mouse.Click(doctor.X + 20, doctor.Y + 15);
                        Thread.Sleep(200);
                        GUI.Keyboard.Write(dataSource[0].Doctor);
                        Thread.Sleep(200);
                        var res = GUI.Screenshot.LocateAllOnScreen("image/诊断.png").First();
                        Thread.Sleep(200);
                        GUI.Mouse.Click(res.X + 40, res.Y + 15);
                        Thread.Sleep(200);
                        GUI.Keyboard.Write(dataSource[0].Res);
                        Thread.Sleep(200);
                        var fee = GUI.Screenshot.LocateAllOnScreen("image/费用.png").First();
                        Thread.Sleep(200);
                        GUI.Mouse.Click(fee.X + 50, fee.Y + 15);
                        Thread.Sleep(200);
                        GUI.Keyboard.Write(dataSource[0].Fee.ToString());
                        Thread.Sleep(200);
                        var otherbtn = GUI.Screenshot.LocateAllOnScreen("image/其他.png").First();
                        Thread.Sleep(200);
                        GUI.Mouse.Click(otherbtn.X + 20, otherbtn.Y + 10);
                        Thread.Sleep(700);
                        var address = GUI.Screenshot.LocateAllOnScreen("image/地址.png").First();
                        Thread.Sleep(200);
                        GUI.Mouse.Click(address.X + 50, address.Y + 15);
                        Thread.Sleep(200);
                        GUI.Keyboard.Write(dataSource[0].Address);
                        Thread.Sleep(200);
                        var savebtn = GUI.Screenshot.LocateAllOnScreen("image/保存按钮.png").First();
                        Thread.Sleep(200);
                        GUI.Mouse.Click(savebtn.X, savebtn.Y);
                    }
                    else
                    {
                        AntdUI.Modal.open(new Modal.Config(this, "请选择病人再进行登记操作！", "", AntdUI.TType.Warn)
                        {
                            CancelText = null,
                            OkText = "确定"
                        });
                    }
                }
                else
                {
                    AntdUI.Modal.open(new Modal.Config(this, "请选择病人再进行登记操作！", "", AntdUI.TType.Warn)
                    {
                        CancelText = null,
                        OkText = "确定"
                    });
                }
            }
        }
    }
}
